package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.user.dao.entity.NoticeMessage;
import com.niiwoo.civet.user.dao.entity.UserBasicInfo;
import com.niiwoo.civet.user.dao.entity.UserInstationLetter;
import com.niiwoo.civet.user.dto.NoticeMessageDTO;
import com.niiwoo.civet.user.dto.request.HomePageMessageHintRequestDTO;
import com.niiwoo.civet.user.dto.response.HomePageMessageHintResponseDTO;
import com.niiwoo.civet.user.service.HomePageDubboService;
import com.niiwoo.civet.user.service.local.HomePageService;
import com.niiwoo.civet.user.service.local.NoticeMessageService;
import com.niiwoo.civet.user.service.local.UserBasicInfoService;
import com.niiwoo.civet.user.service.local.UserLetterService;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by dell on 2017/12/15.
 * Description：civet-parent
 */
@Service(version = "1.0.0")
public class HomePageDubboServiceImpl implements HomePageDubboService {

    @Autowired
    private HomePageService homePageService;
    
    @Autowired
    private UserLetterService userLetterService;
    
    @Autowired
    private NoticeMessageService noticeMessageService;

    @Autowired
    private UserBasicInfoService userBasicInfoService;

    @Autowired
    private PrivacyMasks privacyMasks;
    
    /**
     * 获取最新公告id
     * @return
     */
    @Override
    public Long newestNoticeId() {
        return homePageService.newestNoticeId();
    }


    /**
     * 查询首页公告列表
     * @param pageNumber
     * @param pageSize
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> postNiiwooNoticeList(int pageNumber, int pageSize, String userId) {
        return homePageService.postNiiwooNoticeList(pageNumber, pageSize, userId);
    }


    /**
     * 查看公告详情
     * @param id
     * @return
     */
    @Override
    public NoticeMessageDTO getNoticeDetail(Long id) {
        return homePageService.getNoticeDetail(id);
    }

	@Override
	public HomePageMessageHintResponseDTO homePageMessageHint(HomePageMessageHintRequestDTO requestDTO) {
		HomePageMessageHintResponseDTO responseDTO = new HomePageMessageHintResponseDTO();
		
		// 你我公告
		String mobileNo = null;
		if (StringUtils.hasLength(requestDTO.getUserId())) {
			UserBasicInfo userBasicInfo = userBasicInfoService.selectUserBasicInfo(requestDTO.getUserId());
			mobileNo = privacyMasks.decryptPrivacy(userBasicInfo.getMobileNo());
		}
		NoticeMessage noticeMessage = noticeMessageService.getNewestNoticeMessage(mobileNo);
		if (noticeMessage != null) {
			responseDTO.setNewNoticeId(noticeMessage.getId());
			responseDTO.setNoticeContent(noticeMessage.getSendContent());
		}
		
		// 站内信
		if (StringUtils.hasLength(requestDTO.getUserId())) {
			UserInstationLetter userInstationLetter = userLetterService.getNewestUnreadInstationLetter(requestDTO.getUserId());
			if (null != userInstationLetter) {
				responseDTO.setNewInstationLetterId(userInstationLetter.getId());
				responseDTO.setInstationLetterContent(userInstationLetter.getContent());				
			}
		}
		
		return responseDTO;
	}
}